<?php
/**
 * Created by PhpStorm.
 * Date: 2023/5/06
 * Time: 16:20
 */

namespace app\frontend\models;


use App\common\models\BaseModel;
use Illuminate\Support\Facades\DB;

/**
 * Class MemberAddressModel
 * @package app\common\models
 */
class MemberAddressModel extends BaseModel
{
    public  $table = 'member_address';

    static $_instance; //本类的实例

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    /**
     * 获取用户的地址列表
     * @param $userId
     * @return mixed
     */
    public static function getAddressList($userId)
    {
        return static::select('id', 'username', 'mobile', 'province_id', 'city_id', 'region_id','district', 'address', 'isdefault', 'latitude', 'longitude', 'country_code')
            ->where('uid', $userId)->get()->toArray();
    }

    /**
     * 获取单条数据
     * @param $addressId
     * @return mixed
     */
    public static function getAddressById($addressId)
    {
        return static::where('id', $addressId)->first();
    }

    /**
     * 获取会员收货地址
     * @param $addressId
     * @param $userId
     * @return mixed
     */
    public static function getAddressAndUserById($addressId, $userId)
    {
        return static::where('id', $addressId)->where('uid', $userId)->first();
    }

    /**
     * 通过地址id删除地址
     * @param $addressId
     * @return mixed
     */
    public static function destroyAddress($addressId)
    {
        return static::where('id', $addressId)->delete();
    }

    /**
     * 取消默认模块
     * @param $memberId
     * @return mixed
     */
    public static function cancelDefaultAddress($memberId)
    {
        return static::where('uid', $memberId)->where('isdefault', '1')->update(['isdefault' => '0']);
    }


    /**
     * 保存数据
     * @param $data
     * @return bool
     */
    public function addSave($data)
    {
        if (!$data){
            return '';
        }
        $data['create_time'] = time();
        return DB::table($this->table)->insert($data);
    }

    /**
     * 修改数据
     * @param $condition
     * @param $data
     * @return int|string
     */
    public function updateSave($condition,$data)
    {
        if (!$condition||!$data){
            return '';
        }
        $data['update_time'] = time();
        return DB::table($this->table)->where($condition)->update($data);
    }


    /**
     * 字段规则
     *
     * @return array
     */
     public function rules()
    {
        return [
            'mobile'    => 'regex:/^1[3-9]\d{9}$/',
            'username'  => 'required|max:45|min:2',
            'province_id'  => 'required',
            'city_id'      => 'required',
            'region_id'  => 'required',
            'district'  => 'required|string|max:100',
            'address' => 'required|string|max:255',
            'isdefault' => 'required',
        ];
    }

    /**
     * 字段验证返回错误信息
     *
     * @return array
     */
    static public function message()
    {
        return [
            'username.required' => '收货人不能为空',
            'username.max' => '收货人长度超过最大限制',
            'username.mix' => '收货人长度超过最小限制',
            'mobile.required' => '手机号码不能为空',
            'mobile.regex' => '请输入正确的手机号',
            'province_id.required' => '请选择地区',
            'city_id.required' => '请选择地区',
            'region_id.required' => '请选择地区',
            'district.required' => '请选择地区',
            'district.max' => '地址选择有误',
            'address.required' => '请填写详细地址',
            'address.max' => '详细地址填写有误',
        ];
    }


}
